package com.jk.blog.config.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.Set;

@Component
public class RedisServiceImpl implements RedisService {

//    private static Jedis jedis;
//
//    private static JedisCluster jedisCluster;

    @Autowired
    private JedisConnectionFactory redisConnectionFactory;

    private Jedis getJedis(){
        return redisConnectionFactory.getShardInfo().createResource();
    }

    @Override
    public void del(byte[] key) {
        this.getJedis().del(key);
    }

    @Override
    public void del(String key) {
        this.getJedis().del(key);
    }

    @Override
    public void set(byte[] key, byte[] value) {
        this.getJedis().set(key, value);
    }

    @Override
    public void set(byte[] key, byte[] value, int liveTime) {
        this.set(key, value);
        this.getJedis().expire(key, liveTime);
    }

    @Override
    public void set(String key, String value) {
        this.getJedis().set(key, value);
    }

    @Override
    public void set(String key, String value, int liveTime) {
        this.set(key, value);
        this.getJedis().expire(key, liveTime);
    }

    @Override
    public byte[] get(byte[] key) {
        return this.getJedis().get(key);
    }

    @Override
    public String get(String key) {
        return this.getJedis().get(key);

    }

    @Override
    public Set<String> keys(String pattern) {
        return this.getJedis().keys(pattern);
    }

    @Override
    public boolean exists(String key) {
        return this.getJedis().exists(key);
    }

    @Override
    public String flushDB() {
        return this.getJedis().flushDB();
    }

    @Override
    public long dbSize() {
        return this.getJedis().dbSize();
    }
}
